package com.cai.oamanager.controller;

import com.cai.oamanager.entity.*;
import com.cai.oamanager.service.CurricuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/3/2 23 : 48
 * @package：com.cai.oamanager.controller
 */
@Controller
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private CurricuService curricuService;

    @RequestMapping("/toInScore")
    public String toInScore(HttpServletRequest request,Model model){
        People people = (People) request.getSession().getAttribute("people");
        List<Student> studentsList = curricuService.getStudents(people.getOid());
        model.addAttribute("studentsList",studentsList);
        return "inscore";
    }

    @RequestMapping("/inScore")
    @ResponseBody
    public Curricu inScore(Curricu curricu){
        Integer courseId = curricu.getCourseId();
        Integer stuId = curricu.getStuId();
        Curricu oneCurricu = curricuService.getOneCurricu(stuId, courseId);
        if(oneCurricu==null){
            curricuService.addCurricu(curricu);
            return null;
        }
        return oneCurricu;
    }

    @RequestMapping("/getAllCurricus")
    public @ResponseBody List<Curricu> getAllCurricus(HttpServletRequest request,Model model){
        People people = (People) request.getSession().getAttribute("people");
        System.out.println(people.getOid());
        List<Curricu> allCurricus = curricuService.getAllCurricus(people.getOid(), people.getCid().getId());
        System.out.println("curricus.size "+allCurricus.size());
        return allCurricus;
    }

    @RequestMapping("/updateScore")
    public @ResponseBody String updateScore(Curricu curricu){
        curricuService.updateCurricu(curricu);
        return "success";
    }


    @RequestMapping("/toPlayScore")
    public String toPlayScore(){
        return "playscore";
    }

    @RequestMapping("/jsonObj")
    public @ResponseBody Integer[] jsonObj(HttpServletRequest request){
        People people = (People) request.getSession().getAttribute("people");
//        List<Course> course = curricuService.getCourse(semester, year);
        List<Curricu> allCurricus = curricuService.getAllCurricus(people.getOid(), people.getCid().getId());
        Stream<Curricu> stream = allCurricus.stream();
        Integer[] in = {0,0,0,0};
        stream.map(curricu -> curricu.getScore() * 0.85 + curricu.getWork() * 0.15)
                .forEach( d-> {
                    System.out.println("d::"+d);
                        if(d<60) {
                            in[0] = ++in[0];
                        }else if(d<70){
                            in[1] = ++in[1];
                        }else if(d<80){
                            in[2] = ++in[2];
                        }else if(d<=100){
                            in[3] = ++in[3];
                        }
                });

        return in;
    }

    @RequestMapping("/studentLookScore")
    public @ResponseBody List<Curricu> studentLookScore(HttpServletRequest request, Model model, @RequestParam String semester, @RequestParam String year){
        Student student = (Student) request.getSession().getAttribute("student");
        List<Curricu> curricuList = curricuService.getCurricuStudent(student.getSid(),semester,year);
//        model.addAttribute("curricuList",curricuList);
        return curricuList;
    }

    @RequestMapping("/lookScore")
    public String lookScore(){
        return "lookscore";
    }

}
